package org.rendao.biz.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.apache.log4j.Logger;
import org.rendao.biz.model.FirstIndex;
import org.rendao.biz.model.Question;
import org.rendao.biz.model.QuestionCriteria;
import org.rendao.biz.model.SearchResult;
import org.rendao.biz.model.SecondIndex;
import org.rendao.biz.model.report.QuestionReportEntity;
import org.rendao.biz.service.FirstIndexService;
import org.rendao.biz.service.QuestionService;
import org.rendao.biz.service.SecondIndexService;
import org.rendao.model.CommonSummary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BizController {

	private static final Logger logger = Logger.getLogger(BizController.class);
	
	@Autowired
	private FirstIndexService firstIndexService;
	
	@Autowired
	private SecondIndexService secondIndexService;
	
	@Autowired
	private QuestionService questionService;
	
	@RequestMapping(value = "/first_index/save", method = RequestMethod.POST)
	@ResponseBody
	public FirstIndex saveFirstIndex(@RequestBody FirstIndex firstIndex) throws Exception {
		return firstIndexService.saveOrUpdate(firstIndex);
	}
	
	@RequestMapping(value = "/first_index/get/{id}", method = RequestMethod.GET)
	@ResponseBody
	public FirstIndex getFirstIndexById(@PathVariable("id") String id) throws Exception {
		return firstIndexService.getById(id);
	}
	
	@RequestMapping(value = "/first_index/get_all", method = RequestMethod.GET)
	@ResponseBody
	public SearchResult<FirstIndex> getAllFirstIndex() throws Exception {
		List<FirstIndex> firstIndexes = firstIndexService.getAll();
		SearchResult<FirstIndex> result = 
				SearchResult.byCollection(firstIndexes);
		return result;
	}
	
	@RequestMapping(value = "/first_index/delete/{id}", method = RequestMethod.GET)
	@ResponseBody
	public int deleteFirstIndexById(@PathVariable("id") String id) throws Exception {
		return firstIndexService.deleteById(id);
	}
	
	@RequestMapping(value = "/second_index/get/{id}", method = RequestMethod.GET)
	@ResponseBody
	public SecondIndex getSecondIndexById(String id) throws Exception {
		return secondIndexService.getById(id);
	}
	
	@RequestMapping(value = "/second_index/save", method = RequestMethod.POST)
	@ResponseBody
	public SecondIndex saveSecondIndex(@RequestBody SecondIndex secondIndex) throws Exception {
		return secondIndexService.saveOrUpdate(secondIndex);
	}
	
	@RequestMapping(value = "/second_index/get_by_first_index/{first_index_id}", method = RequestMethod.GET)
	@ResponseBody
	public SearchResult<SecondIndex> getSecondIndexByFirstIndex(@PathVariable("first_index_id")String firstIndexId) throws Exception {
		List<SecondIndex> list = secondIndexService.getByFirstIndex(firstIndexId);
		SearchResult<SecondIndex> result = SearchResult.byCollection(list);
		return result;
	}
	
	@RequestMapping(value = "/second_index/get_all", method = RequestMethod.GET)
	@ResponseBody
	public SearchResult<SecondIndex> getAll() throws Exception {
		List<SecondIndex> list = secondIndexService.getAll();
		SearchResult<SecondIndex> result =  SearchResult.byCollection(list);
		return result;
	}
	
	@RequestMapping(value = "/second_index/delete/{id}", method = RequestMethod.GET)
	@ResponseBody
	public int deleteSecondIndexById(@PathVariable("id")String id) throws Exception {
		return secondIndexService.deleteById(id);
	}
	
	@RequestMapping(value = "/question/get/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Question getQuestionById(@PathVariable("id")String id) throws Exception {
		return questionService.getById(id);
	}
	
	@RequestMapping(value = "/question/save", method = RequestMethod.POST)
	@ResponseBody
	public Question saveQuestion(@RequestBody Question question) throws Exception {
		return questionService.saveOrUpdate(question);
	}
	
	@RequestMapping(value = "/question/search", method = RequestMethod.POST)
	@ResponseBody
	public SearchResult<Question> search(@RequestBody QuestionCriteria criteria) throws Exception {
		List<Question> list = questionService.search(criteria);
		SearchResult<Question> result = SearchResult.byCollection(list);
		return result;
	}
	
	@RequestMapping(value = "/question/report/{format}", method = RequestMethod.POST)
	public ModelAndView genReport(@RequestBody QuestionCriteria criteria,
			@PathVariable("format")String format) throws Exception {
		
		logger.debug("generate report...");
		
		List<QuestionReportEntity> reportData = questionService.getReportData(criteria);
		JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(reportData, false);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("dataSource", dataSource);
		model.put("format", format);
		return new ModelAndView("multiReport", model);
	}
	
	@RequestMapping(value = "/question/first_index_summary", method = RequestMethod.GET)
	@ResponseBody
	public SearchResult<CommonSummary> getFirstIndexSummary() {
		List<CommonSummary> list = questionService.getFirstIndexSummary();
		SearchResult<CommonSummary> result = SearchResult.byCollection(list);
		return result;
	}
}
